﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using DotSpatial.Topology;
using DotSpatial.Symbology;
using DotSpatial.Data;
using DotSpatial.Controls;

namespace HAGISV2
{
    [Serializable]
    public class ScatterPointLayer : SingleBaseLayer
    {

        public ScatterPointLayer(string id,FMAPVIEW viewMode,List<ScatterPointShape> scatterShps)
            : base(id)
        {
            _viewMode = viewMode;
            _featureType = FeatureType.Point;
            _selectedShapeIds = new List<string>();

            BuildDataset();

            if (scatterShps != null)
                _scatterShps = scatterShps;
            else
                _scatterShps = new List<ScatterPointShape>();

            SetLayerFeatures();

        }


        List<string> _selectedShapeIds;
        /// <summary>
        /// 所选图形的id
        /// </summary>
        public List<string> SelectedShapeIds
        {
            get { return _selectedShapeIds; }
            set { _selectedShapeIds = value; }
        }




        private List<ScatterPointShape> _scatterShps;

        public List<ScatterPointShape> ScatterShps
        {
            get { return _scatterShps; }
            set
            {
                _scatterShps = value;
                SetLayerFeatures();
            }
        }


        private List<Color> _colorLst;

        /// <summary>
        /// 离散点根据值（如高程值）的不同显示不同的颜色
        /// </summary>
        public List<Color> ColorLst
        {
            get { return _colorLst; }
            set { _colorLst = value; }
        }

        public override int MapLayerCount
        {
            get
            {
                return 1;
            }
        }

        # region 继承的方法

        public override void BuildDataset()
        {
            base.BuildDataset();
        }

        protected override void OnSetLayerOpacity()
        {
            base.OnSetLayerOpacity();
        }


        protected override void OnSetSelectable()
        {
            base.OnSetSelectable();
        }

        protected override void OnSetVisible()
        {
            base.OnSetVisible();
        }

        public override bool OpenLayer()
        {
            this.BuildDataset();

            this.Update();

            this.Render();
            this.DefaultManager.CurrentMap.Invalidate();


            return true;
        }


        public override void Render()
        {
            base.Render();

            _iLayer.SelectionEnabled = false;

            (this._iLayer as MapPointLayer).Symbolizer = new PointSymbolizer(Color.Orange, DotSpatial.Symbology.PointShape.Ellipse, 8);
            (this._iLayer as MapPointLayer).SelectionSymbolizer = new PointSymbolizer(Color.LightGreen, DotSpatial.Symbology.PointShape.Ellipse, 10);
            //SetFeatureDisplayStyle();
        }

        public override void Redraw()
        {
            SetLayerFeatures();

            if (_iLayer != null)
            {
                (_iLayer as MapPointLayer).DataSet.InitializeVertices();
                _iLayer.Invalidate();
            }
        }


        public override bool SaveLayer()
        {
            return base.SaveLayer();
        }

        protected override void OnViewModeChanged()
        {
            this.Redraw();
        }

        public override void Select(IEnvelope tolerant, IEnvelope strict, SelectionMode selectionMode)
        {
        }

        public override void ClearSelection()
        {
            if ((this._iLayer as MapPointLayer).Selection.Count > 0)
                (this._iLayer as MapPointLayer).Selection.Clear();
            this.SelectedShapeIds.Clear();
        }

        public void Select(IEnvelope tolerant, IEnvelope strict, bool isShiftRemoved)
        {
            if (!IsLoaded) return;
            IEnvelope env;

            _selectedShapeIds.Clear();

            if (!isShiftRemoved)
                (this._iLayer as MapPointLayer).Select(tolerant, strict, SelectionMode.Intersects, out env);

            foreach (IFeature item in (this._iLayer as MapPointLayer).Selection.ToFeatureList())
            {
                _selectedShapeIds.Add(item.DataRow["id"].ToString());
            }
        }


        # endregion

        # region 添加删除编辑等操作

        public void AddPoint(List<ScatterPointShape> ptShps)
        {
            foreach (ScatterPointShape item in ptShps)
            {
                this.AddPoint(item, false);
            }
            (this._iLayer as MapPointLayer).DataSet.InitializeVertices();
            (this._iLayer as MapPointLayer).DataSet.UpdateExtent();
            this._iLayer.Invalidate();
        }

        public void AddPoint(ScatterPointShape ptShp)
        {
            this.AddPoint(ptShp, true);
        }

        void AddPoint(ScatterPointShape ptShp, bool isInvalidate)
        {
            this._scatterShps.Add(ptShp.Clone());

            //dataset添加点
            Feature f = new Feature(ptShp.Pt);

            (this._iLayer as MapPointLayer).DataSet.Features.Add(f);

            f.DataRow["id"] = ptShp.Id;
            f.DataRow["desc"] = ptShp.Desc;

            if (isInvalidate)
            {
                (this._iLayer as MapPointLayer).DataSet.InitializeVertices();
                (this._iLayer as MapPointLayer).DataSet.UpdateExtent();
                this._iLayer.Invalidate();
            }
        }


        public void RemovePoint(List<string> ids)
        {
            foreach (string item in ids)
            {
                this.RemovePoint(item, false);
            }

            this._selectedShapeIds.Clear(); ;

            (this._iLayer as MapPointLayer).DataSet.InitializeVertices();
            (this._iLayer as MapPointLayer).DataSet.UpdateExtent();
            this._iLayer.Invalidate();
        }

        public void RemovePoint(string id)
        {
            this.RemovePoint(id, true);
        }

        void RemovePoint(string id, bool isInvalidate)
        {
            this._scatterShps.Remove(this._scatterShps.Single<ScatterPointShape>(r => r.Id == id));

            //dataset
            foreach (Feature f in (this._iLayer as MapPointLayer).DataSet.Features)
            {
                if ((string)f.DataRow["id"] == id)
                {
                    (this._iLayer as MapPointLayer).DataSet.Features.Remove(f);

                    if (isInvalidate)
                    {
                        (this._iLayer as MapPointLayer).DataSet.InitializeVertices();
                        (this._iLayer as MapPointLayer).DataSet.UpdateExtent();
                        this._iLayer.Invalidate();
                    }
                    return;
                }
            }
        }

        public void EditPoint(List<ScatterPointShape> ptShps)
        {
            throw new NotImplementedException();
        }

        public void EditPoint(ScatterPointShape ptShp)
        {
            ScatterPointShape s = this._scatterShps.Single<ScatterPointShape>(h => h.Id == ptShp.Id);
            _scatterShps[_scatterShps.IndexOf(s)] = ptShp.Clone();

            this.EditPoint(ptShp, true);
        }

        void EditPoint(ScatterPointShape ptShp, bool isInvalidate)
        {
            foreach (Feature f in (this._iLayer as MapPointLayer).DataSet.Features)
            {
                if ((string)f.DataRow["id"] == ptShp.Id)
                {
                    f.BasicGeometry.Coordinates[0].X = ptShp.Pt.X;
                    f.BasicGeometry.Coordinates[0].Y = ptShp.Pt.Y;

                    if (isInvalidate)
                    {
                        (this._iLayer as MapPointLayer).DataSet.InitializeVertices();
                        (this._iLayer as MapPointLayer).DataSet.UpdateExtent();
                        this._iLayer.Invalidate();
                    }
                    return;
                }
            }
        }


        # endregion

        #region 私有方法

        /// <summary>
        /// 设置图层中的图形
        /// </summary>
        void SetLayerFeatures()
        {
            FeatureSet fs = dataSet as FeatureSet;
            fs.Features.Clear();

            switch (this._viewMode)
            {
                case FMAPVIEW.NORMAL:
                    for (int i = 0; i < _scatterShps.Count; i++)
                    {
                        fs.Features.Add((Coordinate)_scatterShps[i].Pt.Clone());
                        fs.Features[i].DataRow["id"] = _scatterShps[i].Id;
                        fs.Features[i].DataRow["desc"] = _scatterShps[i].Desc;
                    }
                    break;
                case FMAPVIEW.FRONT:
                    
                    break;
                case FMAPVIEW.RIGHT:
                    
                    break;
                default:
                    break;
            }

        }



        #endregion

    }
}
